package com.zixuan.gulicollege.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zixuan.gulicollege.eduservice.listener.SubjectExcelListener;
import com.zixuan.gulicollege.eduservice.mapper.EduSubjectMapper;
import com.zixuan.gulicollege.eduservice.service.EduSubjectService;
import com.zixuan.gulicollege.entity.eduservice.EduSubject;
import com.zixuan.gulicollege.entity.eduservice.excel.SubjectData;
import com.zixuan.gulicollege.entity.eduservice.subject.OneSubject;
import com.zixuan.gulicollege.entity.eduservice.subject.TwoSubject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zixuan
 * @since 2021-05-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        InputStream is = null;
        try {
            is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有一级分类的数据
        String parentId = "0";
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);

        // 查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", parentId);
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapperTwo);

        List<OneSubject> finalSubjectList = new ArrayList<>(16);

        // 遍历一级分类
        for (int i = 0; i < oneSubjects.size(); i++) {
            // 得到oneSubjects中的每个edu对象
            EduSubject eduSubject = oneSubjects.get(i);

            // 将所有oneSubject中的值放入集合
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);
            finalSubjectList.add(oneSubject);


            // 遍历二级分类
            List<TwoSubject> twoFinalSubjects = new ArrayList<>(16);

            for (int j = 0; j < twoSubjects.size(); j++) {
                EduSubject tSubject = twoSubjects.get(j);
                // 判断二级分类和一级分类ParentId是否相同
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    // 将tSubject值复制到TwoSubject里
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);;
                    twoFinalSubjects.add(twoSubject);
                }

            }

            // 把一级分类下的所有二级分类放入一级分类里
            oneSubject.setChildren(twoFinalSubjects);
        }

        return finalSubjectList;
    }
}
